<?php
/*粉丝管理*/
namespace Admin\Controller;

use Common\Api\Wechat;
use Common\Controller\AuthController;

class FansController extends AuthController{
    protected $config;
    protected $weObj;

    public function _initialize()
    {
        parent::_initialize();
        $re = M('WechatConfig')->where("wid=%d", 1)->find();
        $config = array(
            'appid' => $re['appid'], //填写高级调用功能的app id
            'appsecret' => $re['appsecret'], //填写高级调用功能的密钥
            'wechat_id' => $re['wid']//微信id
        );

        $this->config = $config;
        $this->weObj = new Wechat($this->config);

    }
    //粉丝列表
    public function fansList()
    {

        $fenzu=$this->weObj->getGroup();
        $this->assign('fenzu',$fenzu['groups']);
        $this->assign('title','分组列表');
        $this->display();
    }
    //新增分组
    public function fadd()
    {
        if(IS_POST){
            $fansname=I('post.fansname');
            if($_POST['id'] ==''){
                $arr=$this->weObj->createGroup($fansname);
                if($arr !==false){
                    $this->success('新增成功');
                }else{
                    $this->error('新增失败');
                }
            }else{
                //更新数据
                  $arr=$this->weObj->updateGroup($_POST['id'],$fansname);
                if($arr !==false){
                    $this->success('新增成功');
                }else{
                    $this->error('新增失败');
                }
            }



        }
    }
    //删除分组
    public function fdel()
    {
        $id = I('get.id');
        $re = $this->weObj->deleteGroup($id);
        if ($re == false) {
            $this->error('删除失败');
        } else {
            if ($re['errcode'] == 0) {
                $this->success('删除成功');
                exit;
            } elseif ($re['errcode'] == 45058) {
                $info = '不能修改0/1/2这三个系统默认保留的标签';
            } elseif ($re['errcode'] == 45057) {
                $info = '该标签下粉丝数超过10w，不允许直接删除';
            } elseif ($re['errcode'] == -1) {
                $info = '系统繁忙';
            } elseif ($re['errcode'] == 45016) {
                $info = '系统分组不允许删除和修改';
            } else {
                $info = '未知错误';
            }
            $this->error($info);


        }
    }


    //场景列表
    public function scene()
    {
        $obj=D('Scene');
      if(IS_POST){
          if(!$obj->create()){
              $this->error($obj->getError());
          }else{
              $arr=$this->weObj->getQRCode($_POST['keyword'],$_POST['type'],1000000);
              $codeimg=$this->weObj->getQRUrl($arr['ticket']);
              $arr['codeimg']=$codeimg;
              $arr['secenname']=$_POST['secenname'];
              $arr['keyword']=$_POST['keyword'];
              $arr['type']=$_POST['type'];
              $arr['c_time']=time();
              if($obj->add($arr)){
                  $this->success('新增场景成功');
              }else{
                  $this->error('新增场景失败');
              }
          }
      }else{
          $counts=$obj->where("status=%d",1)->count();
          $page=page($counts,10,5);
          $list=$obj->where("status=%d",1)->limit($page->firstRow,$page->listRows)->select();
          $this->assign('_list',$list);
          $this->assign('page',$page->show());
          $this->display();
      }


    }
    //删除场景
    public function delscene($sid='')
    {
     if(IS_AJAX){
         if( M('Scene')->where("sid=%d",$sid)->save(['status'=>-1])){
             $this->success('删除成功','',IS_AJAX);
         }else{
             $this->error('删除失败','',IS_AJAX);
         }
     }
    }

}











